import { useState, useEffect } from "react";

//自定义hook 必须是函数
function useMouse() {
  const [x, setX] = useState(0);
  const [y, setY] = useState(0);

  const mouseMoveHandler = (event: MouseEvent) => {
    setX(event.clientX);
    setY(event.clientY);
  };

  useEffect(() => {
    //监听鼠标事件
    window.addEventListener("mousemove", mouseMoveHandler);

    //组件销毁时,一定要解绑DOM事件
    return () => {
      window.removeEventListener("mousemove", mouseMoveHandler);
    };
  }, []);
  return { x, y };
}
export default useMouse;
